package yuanshen;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class GameChouKa  extends JFrame implements ActionListener, MouseListener {
        JButton Recharge = new JButton("充值");//充值
        JButton Exchange = new JButton("兑换");//兑换
        JButton GameCard = new JButton("抽卡");//抽卡
        JButton Role = new JButton("查看已有角色");//已抽到的角色

        //充值按钮
        JButton jButton1 = new JButton("6");
        JButton jButton2 = new JButton("3");
        JButton jButton3 = new JButton("68");
        JButton jButton4 = new JButton("128");
        JButton jButton5 = new JButton("328");
        JButton jButton6 = new JButton("628");
        JButton sure = new JButton("确定");
        JButton sure2 = new JButton("确定");



        //创建输入框
        JTextField jTextField = new JTextField();
        //抽卡-单抽，十连抽
        JButton Single = new JButton("单抽");
        JButton ten = new JButton("十连抽");


        static String pool_five[] = {"迪卢克", "温迪", "公子", "钟离", "阿贝多", "魈"};//五星卡池
        static String pool_four[] = {"托马", "班尼特", "安柏", "香菱", "辛焱"};//四星卡池
        static String pool_up[] = {"胡桃"};//up角色
        static int num_double[] = {2};
        static Map<String, Integer> five_role = new HashMap<>();//存放抽到的五星角色
        static Map<String, Integer> four_role = new HashMap<>();//存放抽到的四星角色

        private int baodi_five = 1;
        private int baodi_four = 1;
        private int baodi_up = 1;
        private int count_five;
        private int count_four;
        private int choujiangquan;//抽奖券
        private int yuanshi;//原石
        private String name;

        Scanner scanner = new Scanner(System.in);


        public GameChouKa() {
            //初始化游戏菜单主界面
            InitMenu();
            //初始化菜单框架搭建
            InitFrame();
            //初始化图片
            LoadPicture();

            //给按钮添加鼠标监听事件
            jButton1.addMouseListener(this);
            jButton2.addMouseListener(this);
            jButton3.addMouseListener(this);
            jButton4.addMouseListener(this);
            jButton5.addMouseListener(this);
            jButton6.addMouseListener(this);
            sure.addMouseListener(this);
            Single.addMouseListener(this);
            ten.addMouseListener(this);


            this.setVisible(true);//设置图形界面可视化






        }

        public void InitMenu() {
            this.setSize(1000, 600);//设置大小
            this.setTitle("原神游戏");
            this.setLocationRelativeTo(null);//设置页面居中
            //设置默认取消机制
            this.setLayout(null);
            this.setDefaultCloseOperation(3);//关闭窗口模式
            //this.addMouseListener(this);

        }

        //窗体内部框架
        public void InitFrame() {

            //设置按钮的位置及大小
            Recharge.setBounds(40, 20, 200, 47);
            Exchange.setBounds(280, 20, 200, 47);
            GameCard.setBounds(520, 20, 200, 47);
            Role.setBounds(760, 20, 200, 47);
            //给按钮设置动作监听事件
            Recharge.addActionListener(this);
            Exchange.addActionListener(this);
            GameCard.addActionListener(this);
            Role.addActionListener(this);
            //鼠标监听事件
            sure2.addMouseListener(this);
            //添加到界面中
            this.getContentPane().add(Recharge);
            this.getContentPane().add(Exchange);
            this.getContentPane().add(GameCard);
            this.getContentPane().add(Role);


        }

        //初始化图片
        public void LoadPicture() {


            //游戏背景图--最下面的图片要最后加载，先加载的图片放上面
            JLabel FrameBackground = new JLabel(new ImageIcon("D:\\IDEA_project\\Project_Yuanshen\\Img-yuanshen\\menu.jpg"));
            FrameBackground.setBounds(0, 0, 1000, 600);

            this.getContentPane().add(FrameBackground);//添加到界面中

            //刷新
            this.getContentPane().repaint();

        }


        //创建消息弹窗
        public void createDialog(String content) {
            JDialog jDialog = new JDialog();
            jDialog.setSize(500, 250);
            jDialog.setLocationRelativeTo(null);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLayout(null);
            jDialog.setModal(true);
            JLabel jLabel = new JLabel(content);
            jLabel.setBounds(30, 0, 450, 130);
            jLabel.setFont(new Font("Serif", Font.BOLD, 15));

            jDialog.getContentPane().add(jLabel);
            jDialog.setVisible(true);

        }

        //展示抽到的角色--弹窗显示图片
        public void Chou_Role(String[] pool, int index) {
            JDialog jDialog = new JDialog();
            jDialog.setLocationRelativeTo(null);
            jDialog.setBounds(350, 100, 300, 400);
            JLabel jLabel = new JLabel(new ImageIcon("D:\\IDEA_project\\Project_Yuanshen\\Img-yuanshen\\" + pool[index] + ".jpg"));
            jDialog.getContentPane().add(jLabel);
            jDialog.setVisible(true);

        }


        //抽up角色
        public void give_up() {
            this.baodi_five = 1;
            this.count_five++;
            this.baodi_four = 1;
        }

        //抽五星角色
        public void give_five() {
            if (this.baodi_up == 2) {
                this.give_up();
            } else {
                int n = (int) (Math.random() * 2);
                if (n == 0) {
                    this.give_up();
                } else {
                    int five = (int) (Math.random() * pool_five.length);
                    //createDialog("抽到了五星角色:"+pool_five[five]);
                    Chou_Role(pool_five, five);
                    this.baodi_five = 1;
                    this.count_five++;
                    this.baodi_up = 1;
                    //将抽到的角色放到集合中

                    if (five_role.containsKey(pool_five[five])) {
                        for (String key : five_role.keySet()) {
                            if (key.equals(pool_five[five])) {
                                int count = five_role.get(key);
                                five_role.put(pool_five[five], ++count);
                            }

                        }

                    } else {
                        //如果集合中没有这个角色，就作为第一次添加进去
                        five_role.put(pool_five[five], 1);
                    }


                }
            }
        }


        //抽四星角色
        public void give_four() {
            int four = (int) (Math.random() * pool_four.length);
            //createDialog("抽到了四星角色:"+pool_four[four]);
            Chou_Role(pool_four, four);

            this.count_five++;
            this.baodi_four = 1;
            this.count_four++;
            if (four_role.containsKey(pool_four[four])) {
                for (String key : four_role.keySet()) {
                    if (key.equals(pool_four[four])) {
                        int count = four_role.get(key);
                        four_role.put(pool_four[four], ++count);
                    }

                }

            } else {
                //如果集合中没有这个角色，就作为第一次添加进去
                four_role.put(pool_four[four], 1);
            }
        }

        //抽卡
        //抽卡
        public void chouka(int n) {
            //判断券的数量是否足够
            if (choujiangquan >= n) {
                for (int i = 0; i < n; i++) {
                    if (this.baodi_five == 90) {//90抽出一五星
                        this.give_five();
                    } else if (this.baodi_four == 10) {//十抽之内出一四星
                        this.give_four();
                    } else {
                        int chouka = (int) (Math.random() * 1000);
                        //73  74:6.6%  75:12.6%
                        //从74抽开始，抽到五星的概率每次增加6%
                        if (chouka <
                                (this.baodi_five < 74 ? 6 : ((this.baodi_five - 73) * 60 + 6))) {
                            this.give_five();
                        } else if (chouka > 939) {
                            this.give_four();
                        } else {
                            //createDialog("没有抽到角色");
                            this.baodi_five++;
                            this.baodi_four++;
                        }

                    }
                }
                choujiangquan -= n;
            } else {
                createDialog("抽奖券的数量不足，请充值");

            }

        }

        //查看抽到的角色
        public void select_role() {
            JDialog jDialog = new JDialog();
            jDialog.setBounds(40, 0, 600, 750);
            jDialog.setLocationRelativeTo(null);
            //五星
            int x = 120, y = 100, width = 200, height = 30;
            for (Map.Entry<String, Integer> entry : five_role.entrySet()) {
                //遍历五星角色的集合
                JLabel jLabelRole = new JLabel(entry.getKey() + " " + entry.getValue() + "张");//把角色放到弹窗里
                jLabelRole.setBounds(x, y, width, height);
                jLabelRole.setFont(new Font("Serif", Font.BOLD, 17));
                jDialog.getContentPane().add(jLabelRole);
                //每个集合角色向下放
                y += 80;

            }
            //四星--

            x = 420;
            y = 100;
            for (Map.Entry<String, Integer> entry : four_role.entrySet()) {
                //遍历四星角色的集合
                JLabel jLabelRole = new JLabel(entry.getKey() + " " + entry.getValue() + "张");//把角色放到弹窗里
                jLabelRole.setBounds(x, y, width, height);
                jLabelRole.setFont(new Font("Serif", Font.BOLD, 17));
                jDialog.getContentPane().add(jLabelRole);
                //每个集合角色向下放
                y += 80;

            }
            //加载窗体的背景图片
            JLabel jLabel = new JLabel(new ImageIcon("D:\\IDEA_project\\Project_Yuanshen\\Img-yuanshen\\table.png"));
            jLabel.setBounds(0, 0, 600, 900);
            jDialog.getContentPane().add(jLabel);
            jDialog.setVisible(true);

        }


        @Override
        public void actionPerformed(ActionEvent e) {
            //点击按钮时获取当前对象
            Object object = e.getSource();

            if (object == Recharge) {
                //充值
                JDialog jDialog = new JDialog();
                jDialog.setTitle("充值~");
                //设置按钮的位置及大小
                jDialog.setBounds(20, 80, 760, 400);
                jButton1.setBounds(50, 80, 170, 47);
                jButton2.setBounds(270, 80, 170, 47);
                jButton3.setBounds(490, 80, 170, 47);
                jButton4.setBounds(50, 150, 170, 47);
                jButton5.setBounds(270, 150, 170, 47);
                jButton6.setBounds(490, 150, 170, 47);
                sure.setBounds(212, 260, 280, 47);
                //取消按钮的默认边框
                jButton1.setBorderPainted(false);
                jButton2.setBorderPainted(false);
                jButton3.setBorderPainted(false);
                jButton4.setBorderPainted(false);
                jButton5.setBorderPainted(false);
                jButton6.setBorderPainted(false);
                //设置按钮的背景颜色
                jButton1.setBackground(new Color(255, 250, 205));
                jButton2.setBackground(new Color(255, 250, 205));
                jButton3.setBackground(new Color(255, 250, 205));
                jButton4.setBackground(new Color(255, 250, 205));
                jButton5.setBackground(new Color(255, 250, 205));
                jButton6.setBackground(new Color(255, 250, 205));
                sure.setBackground(new Color(255, 165, 0));


                jButton1.setFont(new Font("Serif", Font.BOLD, 17));
                jButton2.setFont(new Font("Serif", Font.BOLD, 17));
                jButton3.setFont(new Font("Serif", Font.BOLD, 17));
                jButton4.setFont(new Font("Serif", Font.BOLD, 17));
                jButton5.setFont(new Font("Serif", Font.BOLD, 17));
                jButton6.setFont(new Font("Serif", Font.BOLD, 17));
                sure.setFont(new Font("Serif", Font.BOLD, 18));


                jDialog.setLocationRelativeTo(null);
                jDialog.setModal(true);//设置弹窗不关闭无法进行下面的程序

                jDialog.add(jButton1);
                jDialog.add(jButton2);
                jDialog.add(jButton3);
                jDialog.add(jButton4);
                jDialog.add(jButton5);
                jDialog.add(jButton6);
                jDialog.add(sure);

                JLabel RechargeBack = new JLabel(new ImageIcon("D:\\IDEA_project\\Project_Yuanshen\\Img-yuanshen\\img_1.png"));
                RechargeBack.setBounds(0, 0, 960, 400);

                jDialog.getContentPane().add(RechargeBack);


                jDialog.setVisible(true);//设置弹窗可见

            } else if (object == Exchange) {
                //兑换
                JDialog jDialog2 = new JDialog();

                jDialog2.setSize(500, 250);
                jDialog2.setLocationRelativeTo(null);
                jDialog2.setAlwaysOnTop(true);
                jDialog2.setLayout(null);
                jDialog2.setModal(true);
                JLabel jLabel = new JLabel("请输入要兑换成抽奖券的数量：");
                jLabel.setBounds(30, 0, 230, 130);
                jLabel.setFont(new Font("Serif", Font.BOLD, 14));
                //设置文本框
                jTextField.setBounds(230, 53, 220, 30);

                //提交按钮
                sure2.setBounds(150, 150, 200, 37);
                sure2.setFont(new Font("Serif", Font.BOLD, 18));

                JLabel ExchangeBack = new JLabel(new ImageIcon("D:\\IDEA_project\\Project_Yuanshen\\Img-yuanshen\\b.jpeg"));
                ExchangeBack.setBounds(0, 0, 500, 250);


                //添加进弹窗
                jDialog2.getContentPane().add(jLabel);
                jDialog2.getContentPane().add(jTextField);
                jDialog2.getContentPane().add(sure2);
                jDialog2.getContentPane().add(ExchangeBack);
                //设置可视化
                jDialog2.setVisible(true);

            } else if (object == GameCard) {
                //抽卡

                JDialog jDialog = new JDialog();
                jDialog.setBounds(40, 60, 800, 500);

                Single.setBounds(200, 200, 180, 47);
                ten.setBounds(500, 200, 180, 47);
                Single.setFont(new Font("Serif", Font.BOLD, 17));
                ten.setFont(new Font("Serif", Font.BOLD, 17));
                //设置鼠标监听事件

                jDialog.setLayout(null);//取消弹窗的默认布局
                jDialog.setLocationRelativeTo(null);//设置居中
                //jDialog.setModal(true);//设置弹窗不关闭无法进行下面的程序
                JLabel GameCardBack = new JLabel(new ImageIcon("D:\\IDEA_project\\Project_Yuanshen\\Img-yuanshen\\choub.jpg"));
                GameCardBack.setBounds(0, 0, 800, 500);

                //按钮添加到弹窗
                jDialog.add(Single);
                jDialog.add(ten);
                jDialog.getContentPane().add(GameCardBack);
                jDialog.setVisible(true);//设置弹窗可见


            } else if (object == Role) {
                //查看已有对象

                select_role();
            }


        }

        @Override
        public void mouseClicked(MouseEvent e) {
            Object objectButton = e.getSource();
            if (objectButton == jButton1) {
                jButton1.setBorderPainted(true);
                //设置当点击按钮后就把按钮的边框改变
                jButton1.setBorder(BorderFactory.createLineBorder(new Color(255, 165, 0), 4));
                this.yuanshi += (6 * 10 * this.num_double[0]);
                Arrays.fill(this.num_double, 1);

            } else if (objectButton == jButton2) {
                jButton2.setBorderPainted(true);
                jButton2.setBorder(BorderFactory.createLineBorder(new Color(255, 165, 0), 4));
                this.yuanshi += (30 * 10 * this.num_double[0]);
                Arrays.fill(this.num_double, 1);

            } else if (objectButton == jButton3) {
                jButton3.setBorderPainted(true);
                jButton3.setBorder(BorderFactory.createLineBorder(new Color(255, 165, 0), 4));
                this.yuanshi += (68 * 10 * this.num_double[0]);
                Arrays.fill(this.num_double, 1);

            } else if (objectButton == jButton4) {
                jButton4.setBorderPainted(true);
                jButton4.setBorder(BorderFactory.createLineBorder(new Color(255, 165, 0), 4));
                this.yuanshi += (128 * 10 * this.num_double[0]);
                Arrays.fill(this.num_double, 1);

            } else if (objectButton == jButton5) {
                jButton5.setBorderPainted(true);
                jButton5.setBorder(BorderFactory.createLineBorder(new Color(255, 165, 0), 4));
                this.yuanshi += (328 * 10 * this.num_double[0]);
                Arrays.fill(this.num_double, 1);

            } else if (objectButton == jButton6) {
                jButton6.setBorderPainted(true);
                jButton6.setBorder(BorderFactory.createLineBorder(new Color(255, 165, 0), 4));
                this.yuanshi += (648 * 10 * this.num_double[0]);
                Arrays.fill(this.num_double, 1);

            } else if (objectButton == sure) {
                //提交充值按钮
                //sure.setBackground(new Color(255,165,0));
                createDialog("充值成功！当前原石数量为" + this.yuanshi + "个");


            } else if (objectButton == sure2) {
                //提交兑换按钮
                //sure2.setBackground(Color.orange);
                //获取文本框的内容
                int num = Integer.parseInt(jTextField.getText());
                if (this.yuanshi < 160) {
                    createDialog("当前原石的数量不足以兑换！");

                } else {
                    this.choujiangquan += num;
                    this.yuanshi -= (num * 160);
                    createDialog("兑换成功，抽奖券数量为" + this.choujiangquan + "张！");


                }

            } else if (objectButton == Single) {
                //单抽
                chouka(1);

            } else if (objectButton == ten) {
                //十连抽
                chouka(10);

            }


        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {


        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }

}
